var app = getApp()
Page({
  data:{
    orderId:'',
    orderDetails:'',
    telePhone:'',
    room:"",
    refund:'',
    payDetails:'',
    orderPrivilegeInfoList:'',
    refundPrice:'',
    payNum:'',
    roomDetails:'',
    statusText:'',
    finishModal:false,
    privilege:'',
    prePrice:''
  },
  onLoad:function(options){
    this.setData({
      orderId: options.orderId
    })
    this.getOrderDetails();
    console.log(this.data.orderId)
    console.log(wx.getStorageSync(app.globalData.sessionId))
  },
  getOrderDetails:function(){
    var _this=this;
    app.request({
      url: app.globalData.apiUrl +'/mobileOfficing/api/orderDetail?orderId=' + _this.data.orderId,
      method:'POST',
      header:{
        'content-type':'application/x-www-form-urlencoded',
        'sessionId':wx.getStorageSync(app.globalData.sessionId)
      },
      success:function(res){
        console.log(res)
        if(res.data.result){
          _this.setData({
            orderDetails: res.data.data.orderDetail,
            orderPrivilegeInfoList: res.data.data.orderPrivilegeInfoList,
            room: res.data.data.room,
            refund: res.data.data.refund,
            payDetails: res.data.data.payDetails,
            roomDetails: res.data.data.roomDetails
          })
          _this.getDataType();
          _this.orderStatus();
          _this.privilegeInfo();
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel:false,
            confirmText:"知道了",
            success: function (reg) {
              if (reg.confirm) {
                console.log('用户点击确定')
              } 
            }
          })
        }
      }
    })
  },
  scrollTo:function(){
    var _this=this;
      var query = 
        wx.createSelectorQuery().select('.f_mniu').boundingClientRect(function(res){
          console.log(res)
          var top=res.top
          if (_this.data.orderDetails.payMent==3){
            top=top-110;
          }else{
            top=top-90
          }
          wx.pageScrollTo({
            scrollTop: top
          })
        }).exec()
    
  },
  getDataType:function(){
    var _this=this;
    //退款金额总数计算
    var refundNum=0;
    _this.data.refund.forEach(function (val, index, arr) {
      refundNum += parseFloat(val.amount)
    })
    refundNum = refundNum.toFixed(2);
    _this.setData({
      refundPrice: refundNum
    })
    //电话号码中间四位加密
    if (_this.data.orderDetails){
      _this.setData({
        telePhone: _this.data.orderDetails.telephone.substr(0, 3) + '****' + _this.data.orderDetails.telephone.substr(7)
      })
    }
    //收款信息金额分换成元并保留两位小数
    if (_this.data.payDetails){
      var num = _this.data.payDetails.totalFee / 100
      _this.setData({
        payNum: num.toFixed(2)
      })
    }
  },
  //优惠政策
  privilegeInfo: function () {
    var _this = this;
    _this.data.orderPrivilegeInfoList.forEach(function (val, index, arr) {
      if (val.preferentialType == 0) {
        //优惠类型为支付类型
        if (val.payType==1){
          //现付
          if (val.rule==0){
          //折扣
            if (val.percentage){
              _this.setData({
                privilege: '现付（' + (val.percentage) / 10 +'折）'
              })
              val.privilegeText = _this.data.privilege
            }
          } else if (val.rule==1){
          //立减
            if (val.percentage) {
              _this.setData({
                privilege: '现付 （立减' + val.percentage + '元）'
              })
              val.privilegeText = _this.data.privilege
            }
          }
        } else if (val.payType == 2){
          //预付
          if (val.rule == 0) {
            //折扣
            if (val.percentage) {
              _this.setData({
                privilege: '预付（' + (val.percentage) / 10 + '折）'
              })
              val.privilegeText = _this.data.privilege
            }
          } else if (val.rule == 1) {
            //立减
            if (val.percentage) {
              _this.setData({
                privilege: '预付 （立减' + val.percentage + '元）'
              })
              val.privilegeText = _this.data.privilege
            }
          }
        }
      } else if (val.preferentialType == 1) {
        //优惠类型为连住优惠
        if (val.rule == 0) {
          //折扣
          if (val.percentage) {
            _this.setData({
              privilege: '连住' + val.overDays + '天（' + (val.percentage) / 10 + '折）'
            })
            val.privilegeText = _this.data.privilege
          }
        } else if (val.rule == 1) {
          //立减
          if (val.percentage) {
            _this.setData({
              privilege: '连住' + val.overDays + '天 （立减' + val.percentage + '元）'
            })
            val.privilegeText = _this.data.privilege
          }
        }

      } else if (val.preferentialType == 2) {
        //优惠类型为提前预订
        if (val.rule == 0) {
          //折扣
          if (val.percentage) {
            _this.setData({
              privilege: '提前预订' + val.overDays + '天（' + (val.percentage) / 10 + '折）'
            })
            val.privilegeText = _this.data.privilege
          }
        } else if (val.rule == 1) {
          //立减
          if (val.percentage) {
            _this.setData({
              privilege: '提前预订' + val.overDays + '天 （立减' + val.percentage + '元）'
            })
            val.privilegeText = _this.data.privilege
          }
        }
      }
    })

    _this.setData({
      orderPrivilegeInfoList: _this.data.orderPrivilegeInfoList
    })

  },
  //所有按钮隐藏操作
  hideButton:function(){
    var _this=this;
    _this.setData({
      statusNum12:false,
      statusNum15:false,
      statusNum16: false,
      statusNum24: false,
      statusNum25: false,
      statusNum34: false,
      statusNum35: false
    })
  },
  //订单状态判断
  orderStatus:function(){
    var _this=this;
    var payment = _this.data.orderDetails.payMent;
    var producstatus = _this.data.orderDetails.productStatus;
    if (payment==1){
      //现付
      if (producstatus==2){
        _this.setData({
          statusNum12:true,
          statusText:'为保证服务质量，请您及时确认订单'
        })
      } else if (producstatus==6){
        _this.setData({
          statusNum16: true,
          statusText: '客户申请取消订单，请您及时处理'
        })

      } else if (producstatus == 5) {
        _this.setData({
          statusNum15: true,
          statusText: '您已确认订单，请为客户预留房间'
        })

      } else if (producstatus == 7) {
        _this.setData({
          statusNum17: true,
          statusText: '客户已离店，本次交易完成'
        })

      } else if (producstatus == 8) {
        _this.setData({
          statusNum18: true,
          statusText: '很遗憾，本次交易失败'
        })

      }
    } else if (payment == 2){
      //预付
      if (producstatus == 3) {
        _this.setData({
          statusNum23: true,
          statusText: '客户尚未支付房费，请耐心等待'
        })
      } else if (producstatus == 4) {
        _this.setData({
          statusNum24: true,
          statusText: '为保证服务质量，请您及时确认订单'
        })

      } else if (producstatus == 5) {
        _this.setData({
          statusNum25: true,
          statusText: '您已确认订单，请为客户预留房间'
        })

      } else if (producstatus == 7) {
        _this.setData({
          statusNum27: true,
          statusText: '客户已离店，本次交易完成'
        })

      } else if (producstatus == 8) {
        _this.setData({
          statusNum28: true,
          statusText: '很遗憾，本次交易失败'
        })

      }

    } else if (payment == 3){
      //担保
      if (producstatus == 3) {
        _this.setData({
          statusNum33: true,
          statusText: '客户尚未支付担保金，请耐心等待'
        })
      } else if (producstatus == 4) {
        _this.setData({
          statusNum34: true,
          statusText: '为保证服务质量，请您及时确认订单'
        })

      } else if (producstatus == 5) {
        _this.setData({
          statusNum35: true,
          statusText: '您已确认订单，请为客户预留房间'
        })

      } else if (producstatus == 7) {
        _this.setData({
          statusNum37: true,
          statusText: '客户已离店，本次交易完成'
        })

      } else if (producstatus == 8) {
        _this.setData({
          statusNum38: true,
          statusText: '很遗憾，本次交易失败'
        })
      }
    }
  },
  //确认订单弹窗及造作
  showModalSure:function(){
    var _this=this;
    wx.showModal({
      title: '确认订单',
      content: '是否确认订单？确认后，请预留房间！',
      cancelText:'再想想',
      cancelColor:'#333',
      confirmText:'确认',
      confirmColor:'#2e85f5',
      success: function (res) {
        if (res.confirm) {
          app.request({
            url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/verifyOrder?orderId=' + _this.data.orderId,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'sessionId': wx.getStorageSync(app.globalData.sessionId)
            },
            success: function (res) {
              if(res.data.result){
                console.log('订单确认完成')
                _this.hideButton();
                _this.getOrderDetails();
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "知道了",
                  success: function (reg) {
                    if (reg.confirm) {
                      console.log('用户点击确定')
                      _this.getOrderDetails();
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },
  //同意买家取消订单弹窗及操作
  showModalAgree:function(){
    var _this = this;
    wx.showModal({
      title: '同意取消订单',
      content: '买家已申请取消订单，是否同意取消？',
      cancelText: '再想想',
      cancelColor: '#333',
      confirmText: '同意',
      confirmColor: '#2e85f5',
      success: function (res) {
        if (res.confirm) {
          app.request({
            url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/consentUserCancelOrder?orderId=' + _this.data.orderId,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'sessionId': wx.getStorageSync(app.globalData.sessionId)
            },
            success: function (res) {
              if (res.data.result) {
                console.log('同意取消订单成功')
                _this.hideButton();
                _this.getOrderDetails();
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "知道了",
                  success: function (reg) {
                    if (reg.confirm) {
                      console.log('用户点击确定')
                      _this.getOrderDetails();
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },
  //完成按钮点击，完成弹窗显示
  showModalfinish:function(){
    var _this=this;
    if (_this.data.orderDetails.payMent==3){
      _this.setData({
        finishModal:true
      })
    }else{
      wx.showModal({
        title: '完成订单',
        content: '确认客户已离店？确认后，订单交易完成。',
        cancelText: '再想想',
        cancelColor: '#333',
        confirmText: '完成',
        confirmColor: '#2e85f5',
        success: function (res) {
          if (res.confirm) {
            app.request({
              url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/completeOrder?orderId=' + _this.data.orderId,
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'sessionId': wx.getStorageSync(app.globalData.sessionId)
              },
              success: function (res) {
                if (res.data.result) {
                  console.log('同意取消订单成功')
                  _this.hideButton();
                  _this.getOrderDetails();
                } else {
                  wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false,
                    confirmText: "知道了",
                    success: function (reg) {
                      if (reg.confirm) {
                        console.log('用户点击确定')
                        _this.getOrderDetails();
                      }
                    }
                  })
                }
              }
            })
          } else if (res.cancel) {
            return;
          }
        }
      })
    }
  },
  //担保-完成订单-点击取消
  cancleFinish: function () {
    var _this=this;
    _this.setData({
      finishModal: false
    })
  },
  //担保-完成订单-点击确定及操作
  finished:function(){
    var _this=this;
    app.request({
      url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/completeOrder?orderId=' + _this.data.orderId,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'sessionId': wx.getStorageSync(app.globalData.sessionId)
      },
      success: function (res) {
        if (res.data.result) {
          console.log('完成订单成功')
          _this.hideButton();
          _this.getOrderDetails();
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            confirmText: "知道了",
            success: function (reg) {
              if (reg.confirm) {
                _this.setData({
                  finishModal: false
                })
                _this.getOrderDetails();
              }
            }
          })
        }
      }
    })
  },
  //返回订单列表
  backToOrderlist:function(){
    var _this=this;
    wx.redirectTo({
      url: '../come/come?plateId=' + _this.data.orderDetails.plateId
    })
  },
  //取消订单页面跳转
  goToCancelorder:function(){
    var _this = this;
    if (_this.data.orderDetails.payMent==3){
      wx.redirectTo({
        url: '../Cancellation/Cancellation?orderId=' + _this.data.orderId + '&guarantyMoney=' + _this.data.orderDetails.assureTotal + '&oldreceivedAmount=' + _this.data.orderDetails.amount
      })
    }else{
      wx.redirectTo({
        url: '../cancelorder/cancelorder?orderId=' + _this.data.orderId
      })
    }
  },
  //订单退款页面跳转
  goToRefundorder:function(){
    var _this=this;
    wx.redirectTo({
      url: '../refund/refund?orderId=' + _this.data.orderId + '&receivedAmount=' + _this.data.orderDetails.receivedAmount
    })
  },
  //修改订单页面跳转
  goToChangeorder:function(){
    var _this = this;
    if (_this.data.orderDetails.payMent == 2){
      wx.redirectTo({
        url: '../modify/modify?orderId=' + _this.data.orderId + '&oldReceivedAmount=' + _this.data.orderDetails.receivedAmount
      })
    }else{
      wx.redirectTo({
        url: '../modify_list/modify_list?orderId=' + _this.data.orderId + '&oldAmount=' + _this.data.orderDetails.amount + '&oldReceivedAmount=' + _this.data.orderDetails.receivedAmount
      })
    }
  },
  //拨打电话
  callmeTap:function(){
    var _this=this;
    wx.makePhoneCall({
      phoneNumber: _this.data.orderDetails.telephone
    })

  }

    
})
